<!-- 厂家-确认送达 -->
<template>
	<ListLayout>
		<!-- 附件 -->
		<AttachmentCard ref="AttachmentCard"></AttachmentCard>
		<!-- 设备接受确认 -->
		<ServiceDeviceCard ref="ServiceDeviceCard" :defaultData="data" :source="Source"></ServiceDeviceCard>
		<!-- 签名 -->
		<SignCard ref="SignCard"></SignCard>

		<template #footer>
			<HButtonGroup :rightPlain="false" @rightClick="rightClick" @leftClick="leftClick">
				<template #left>
					取消
				</template>
				<template #right>
					确认送达
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>
</template>

<script>
	import ListLayout from '@/components/layout/listLayout.vue'

	import AttachmentCard from '@/components/card/attachment.vue'
	import ServiceDeviceCard from '@/components/card/sendRepair/factory/info/serviceDevice.vue'
	import SignCard from '@/components/card/sign.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'

	import ReapirApi from '@/apis/repair.js'
	import { mathSum } from '@/filter/index.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	import SendRepairRouterConfig from '@/sendRepair/routerConfig.js'
	export default {
		components: {
			ListLayout,
			AttachmentCard,
			ServiceDeviceCard,
			SignCard,
			HButtonGroup,
		},

		computed: {
			Source() {
				return this.routerParams.source
			},
		},
		data() {
			return {
				routerParams: {},
				data: {},
			}
		},
		async onLoad(query) {
			const { state, currStateToName, id, source } = query
			this.$set(this.routerParams, 'text', currStateToName || '')
			this.$set(this.routerParams, 'state', state || '')

			this.$set(this.routerParams, 'id', id)
			this.$set(this.routerParams, 'source', source || '')
			this.init()

		},
		methods: {
			async init() {
				wx.showLoading({
					title: '加载中...',
					mask: true
				})
				let res = await ReapirApi.common.getDetail(this.routerParams.id)
				if (res.code === 0) {
					this.data = res.body || {}
					this.$set(this.routerParams, 'source', this.data.source || '')

				}
				wx.hideLoading()
			},
			toast() {
				wx.showToast({
					title: '送达成功!',
					icon: 'success',
					duration: 2000,
					complete() {
						wx.redirectTo({
							url: SendRepairRouterConfig.Factory.Index
						})
					}
				})
			},
			rightClick() {
				let { AttachmentCard, ServiceDeviceCard, SignCard } = this.$refs
				let { user, phone, situation, appearance, state, test, voiceReturn, voiceType, voiceNo, settlementMethod } = ServiceDeviceCard.$data.formData
				let attachment = AttachmentCard.$data.attachment

				SignCard.upSign(async (file) => {
					let params = {
						// 送修id
						repairId: this.routerParams.id,
						// 维修结果（1.已完成 2.完成部分 3.未完成）
						repairResult: situation,
						// 结算方式
						settlementMethod: settlementMethod,
						// 设备状态外观
						deviceState: appearance,
						// 设备数据状态
						deviceData: state,
						// 是否有发票（0.否，1.是）
						invoiceFlag: voiceReturn || 0,
						// 发票类型
						invoiceType: voiceType,
						// 发票号
						invoiceNum: voiceNo,
						// 发票附件
						invoiceEnclosure: '',
						// 是否有测试报告（0.否，1.是）
						testReportFlag: test || 0,
						// 测试报告附件
						testReportEnclosure: '',
						// 附件
						enclosure: attachmentFormatter(attachment),
						// 接收人/取走人
						receiveUser: user,
						// 接收人电话/取走人电话
						receivePhone: phone,
						// 签名人
						signUser: '',
						// 签名地址
						signAddress: file?.url || '',
						// 服务态度
						serviceAttitude: '',
						// 服务质量
						serviceQuality: '',
						// 响应时效
						responsePrescription: '',
						// 意见与建议
						commentSuggestion: '',
						// 评价结果
						evaluateResult: '',
					}
					await ReapirApi.department.confirmService(params)
					this.toast()
				})
			},
			leftClick() {
				wx.navigateBack()
			}
		}
	}
</script>

<style>
</style>
